﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;

namespace LevenBlog.Data.SqlServer
{
    public sealed class SqlImageType : IImageType
    {
        public int Count(IDbProvider provider)
        {
            string sql = "select count([id]) from [lb_imagetype]";
            return Convert.ToInt32(SqlHelper.Instance.ExecuteScalar(provider, sql));
        }

        public void Insert(IDbProvider provider, lb_imagetype type)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateInsertSql(type, out paras);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Update(IDbProvider provider, lb_imagetype type)
        {
            IDataParameter[] paras = null;
            string sql = SqlHelper.Instance.CreateUpdateSql(type, out paras);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Delete(IDbProvider provider, int id)
        {
            string sql = string.Format("delete from [lb_imagetype] where [id]={0}", id);
            SqlHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public lb_imagetype Get(IDbProvider provider, int id)
        {
            string sql = string.Format("select * from [lb_imagetype] where [id]={0}", id);
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToEntity<lb_imagetype>(dr);
            }
        }

        public IList<lb_imagetype> Get(IDbProvider provider)
        {
            string sql = "select * from [lb_imagetype] order by [id] asc";
            using (IDataReader dr = SqlHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqlHelper.Instance.ConvertToList<lb_imagetype>(dr);
            }
        }
    }
}
